package com.example.viewpager2_fragment;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.example.viewpager2_fragment.adapter.FragmentAdapter;
import com.example.viewpager2_fragment.fragment.BlankFragment;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    ViewPager2 viewPager2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    /*    ViewPager2 viewPager2 = findViewById(R.id.myViewPager);
        viewPager2.setAdapter(new ViewPager2Adapter());*/

        initPage();
    }


    private void initPage() {
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(BlankFragment.newInstance("我最帅"));
        fragments.add(BlankFragment.newInstance("我最丑"));
        fragments.add(BlankFragment.newInstance("我很美"));
        fragments.add(BlankFragment.newInstance("我很壮"));

        viewPager2 = findViewById(R.id.myViewPager);
        viewPager2.setAdapter(new FragmentAdapter(getSupportFragmentManager(), getLifecycle(), fragments));

        new TabLayoutMediator(findViewById(R.id.mTabLayout), viewPager2, (tab, position) -> {
            switch (position) {
                case 0:
                    tab.setText("1");
                    break;
                case 1:
                    tab.setText("2");
                    break;
                case 2:
                    tab.setText("3");
                    break;
                case 3:
                    tab.setText("4");
                    break;
                case 4:
                    tab.setText("5");
                    break;
            }
        }).attach();

    }

}